package com.kun.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.CarrentalMaker;
import com.kun.rental.service.ICarrentalMakerService;
import com.kun.rental.utils.PinYinUtils;
import com.kun.rental.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *租车制造商控制器类，用于处理与租车相关的请求。
 * @author ikun
 * @since 2024-07-04
 */

@RestController
@RequestMapping("/rental/carrentalMaker")
public class CarrentalMakerController {

    @Autowired
    private ICarrentalMakerService carrentalMakerService;

    /**
     *
     * 分页查询
     * @return
     */
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody CarrentalMaker carrentalMaker){

       Page<CarrentalMaker> page = carrentalMakerService.search(start,size,carrentalMaker);
        return Result.success().setData(page);
    }

    /**
     * 通过id删除信息
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        return carrentalMakerService.removeByIds(list)? Result.success() : Result.fail();
    }


    /**
     *新增信息
     * @param carrentalMaker
     * @return
     */
    @PostMapping
    public Result save(@RequestBody CarrentalMaker carrentalMaker) {
        carrentalMaker.setOrderLetter(PinYinUtils.getPinYin(carrentalMaker.getName()));
        return carrentalMakerService.save(carrentalMaker) ? Result.success() : Result.fail();
    }

    /**
     * 修改信息
     * @param carrentalMaker
     * @return
     */
    @PutMapping
    public Result update(@RequestBody CarrentalMaker carrentalMaker) {
        carrentalMaker.setOrderLetter(PinYinUtils.getPinYin(carrentalMaker.getName()));
        return carrentalMakerService.updateById(carrentalMaker) ? Result.success() : Result.fail();
    }



    // 查询所有数据
    @GetMapping
    public Result selectAll() {
        return Result.success().setData(carrentalMakerService.list());
    }





}
